﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.service;
using DevExpress.Data;
using DXApplicationTangche.UC.门店下单.DTO;
using DXApplicationTangche.UC.款式异常;
using DXApplicationTangche.UC.门店下单.form.订单修改;
using DevExpress.XtraEditors.Popup;
using DevExpress.Utils.Win;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DiaoPaiDaYin;
using DXApplicationTangche.model;
using DXApplicationTangche.DTO;
using DevExpress.XtraGrid.Demos.util;
using mendian;
using DXApplicationTangche.UC.订单.service;
using DXApplicationTangche.UC.订单一览新.订单预览新;

namespace DXApplicationTangche.UC.门店下单.form
{

    public partial class Frm订单一览 : DevExpress.XtraEditors.XtraForm
    {
        public List<OrderStatusIndexDTOShow> orderStatusIndexDTOShows { get; set; } = new List<OrderStatusIndexDTOShow>();
        private List<OrderStatusIndexDTO> orderStatusIndexDTOs = new List<OrderStatusIndexDTO>();
        public Enum订单类别 enum订单类别 = Enum订单类别.销售订单;
        public enum Enum订单类别 { 销售订单, 样品订单, 坚山订单, 坚山美国订单, 套号订单, Luna店铺订单, 坚山上海订单,坚山XML };//订单类型
        
        public Frm订单一览(Enum订单类别 @enum)
        {
            this.enum订单类别 = @enum;
            InitializeComponent();
        }
        
        private void Frm已付款订单一览_Load(object sender, EventArgs e)
        {
            if (this.enum订单类别 == Enum订单类别.Luna店铺订单)
            {
                this.orderStatusIndexDTOs = FunctionHelper.JsonDeserialization<List<OrderStatusIndexDTO>>(RestCall.httpGetMethod(URL.接口url + "/pmc/getOrderStatusIndexPageable?shopId=51&pageStart=0&pageSize=100"));
                foreach (OrderStatusIndexDTO dto in this.orderStatusIndexDTOs)
                {
                    this.orderStatusIndexDTOShows.Add(new OrderStatusIndexDTOShow(dto));
                }
                this.gridControl订单一览.DataSource = this.orderStatusIndexDTOShows;
            }
            this.gridView1.Columns["ORDER_DATE"].SortOrder = ColumnSortOrder.Descending;
            this.gridView1.ExpandAllGroups();
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            String orderId = FunctionHelper.GetgridViewValue(this.gridView1, e.RowHandle, "ORDER_ID").ToString();
            new Form订单预览新(orderId).ShowDialog();
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void refreshData() {
            if (this.enum订单类别 == Enum订单类别.Luna店铺订单)
            {
                this.orderStatusIndexDTOs = FunctionHelper.JsonDeserialization<List<OrderStatusIndexDTO>>(RestCall.httpGetMethod(URL.接口url + "/pmc/getOrderStatusIndexPageable?shopId=51&pageStart=0&pageSize=100"));
                foreach (OrderStatusIndexDTO dto in this.orderStatusIndexDTOs)
                {
                    this.orderStatusIndexDTOShows.Add(new OrderStatusIndexDTOShow(dto));
                }
                this.gridControl订单一览.DataSource = this.orderStatusIndexDTOShows;
            }
            this.gridView1.Columns["ORDER_DATE"].SortOrder = ColumnSortOrder.Descending;
            this.gridView1.ExpandAllGroups();
            this.gridView1.RefreshData();
        }


        #region 选择门店
        //private String status_code;
        //private String status_name;
        //private void searchLookUpEdit1_Popup(object sender, EventArgs e)
        //{
        //    //得到当前SearchLookUpEdit弹出窗体
        //    PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
        //    SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
        //    LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
        //    //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
        //    if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
        //    {
        //        //得到空的空间
        //        EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

        //        ////得到取消按钮，重写点击事件
        //        //Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
        //        //LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
        //        //clearBtn.Click += clearBtn_Click;

        //        //添加一个simplebutton控件(确认按钮)
        //        //LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
        //        //myLCI.TextVisible = false;
        //    }
        //}

        //private void searchLookUpEdit1View_Click(object sender, EventArgs e)
        //{
        //    var a = ((SearchLookUpEdit)sender).Properties.View.GetSelectedRows();
        //    foreach (int rowHandle in a)
        //    {
        //        try
        //        {
        //            this.status_code //  no
        //                = ((SearchLookUpEdit)sender).Properties.View.GetRowCellValue(rowHandle, "code").ToString();//id 是 Value Member
        //        }
        //        catch { }

        //        this.status_name //  no
        //            = ((SearchLookUpEdit)sender).Properties.View.GetRowCellValue(rowHandle, "name").ToString();//id 是 Value Member
        //    }

        //    this.refreshData();
        //}

        ///// <summary>
        ///// 清除按钮事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void clearBtn_Click(object sender, EventArgs e)
        //{
        //    this.searchLookUpEdit1.ToolTip = null;
        //    this.searchLookUpEdit1.EditValue = null;
        //}
        //private void searchLookUpEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        //{
        //    if (null != e.Value)
        //        e.DisplayText = this.status_name;
        //}
        #endregion

        private void 取消订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String orderId = gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, "ORDER_ID").ToString();
                DTO返回值判断 dTO返回值判断 = 订单Service.get清单取消判断(orderId);
                if ("200".Equals(dTO返回值判断.errorCode))
                {
                    RestCall.PostHttp(URL.接口url + "/pmc/orderCancel", orderId);
                    MessageBox.Show("已改为取消订单");
                }
                else
                {
                    MessageBox.Show(dTO返回值判断.errorMessage);
                }
            }
            catch
            {
                MessageBox.Show("修改订单状态失败");
            }
        }

    }

}